package com.oyjd.business.listview.activity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.oyjd.R;
import com.oyjd.business.listview.service.ChengJiService;
import com.oyjd.business.listview.vo.FileVO;
import com.oyjd.framework.Const;
import com.oyjd.framework.log.MyLog;
import com.oyjd.framework.service.CallBack;
import com.oyjd.framework.ui.listview.ListViewEx;
import com.oyjd.framework.ui.title.TitleBar;
import com.oyjd.framework.util.MsgShow;

/**
 * 文件列表
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
@SuppressLint("SimpleDateFormat")
public class ChengJiFileListActivity extends Activity {
	private static final String TAG = "ChengJiFileListActivity";
	private LayoutInflater flater;// 布局加载器

	private ListViewEx listview;
	private TitleBar titleBar;

	private ChengJiService service = new ChengJiService();
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 初始化布局加载器
		flater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		setContentView(R.layout.act_file_list);

		// 界面元素初始化
		titleBar = (TitleBar) this.findViewById(R.id.titleBar);
		titleBar.setBackVisiable(false);
		titleBar.setRightVisiable(true);
		titleBar.setTitleText("文件列表");
		titleBar.title_right_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ChengJiFileListActivity.this, ChengJiEditActivity.class);
				startActivityForResult(intent, Const.ChengJiFileListActivity_code);
			}
		});

		listview = (ListViewEx) this.findViewById(R.id.listview);
		listview.setPageSize(service.pageSize);
		listview.setBackgroundColor(Color.WHITE);
		listview.setOnListViewExListener(new ListViewEx.ListViewExListener() {

			@Override
			public void onRefreshClick() {
				refresh();
			}

			@Override
			public void onMoreClick() {
				service.getMoreData(new CallBack() {

					@SuppressWarnings("unchecked")
					@Override
					public void back(Object data) {
						List<FileVO> list = (List<FileVO>) data;
						List<View> views = listview.getViews();
						for (FileVO vo : list) {
							views.add(createItemView(vo));
						}
						listview.notifyMoreFinished();

					}
				});
			}
		});
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				FileVO vo = (FileVO) arg1.getTag();
				Intent intent = new Intent(ChengJiFileListActivity.this, ShowCountActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("fileName", vo.getFileName());
				bundle.putString("msg", service.countByFile(vo.getFileName()));
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				final FileVO vo = (FileVO) arg1.getTag();
				Builder builder = new AlertDialog.Builder(ChengJiFileListActivity.this);
				builder.setTitle("警告");
				builder.setMessage("确认要删除 [" + vo.getFileName() + "] 文件吗？");
				builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface d, int i) {
						new File(Const.APP_DATA_DIR + vo.getFileName()).delete();
						refresh();
					}
				});

				builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface d, int i) {

					}
				});
				AlertDialog dialog = builder.create();
				dialog.show();
				return false;
			}
		});

		// 加载初始化数据
		refresh();
	}

	/**
	 * 刷新
	 */
	private void refresh() {
		service.getFirstData(new CallBack() {

			@SuppressWarnings("unchecked")
			@Override
			public void back(Object data) {
				List<FileVO> list = (List<FileVO>) data;
				List<View> views = listview.getViews();
				views.clear();
				for (FileVO vo : list) {
					views.add(createItemView(vo));
				}
				listview.notifyRefreshFinished();
			}
		});
	}

	/**
	 * 根据数据创建视图
	 * 
	 * @param vo
	 * @return
	 */
	private View createItemView(FileVO vo) {
		View view = flater.inflate(R.layout.file_item, null);
		((TextView) view.findViewById(R.id.lv_tv_m1_author)).setText("共有" + vo.getCount() + "人");
		((TextView) view.findViewById(R.id.lv_tv_m1_title)).setText(vo.getFileName());
		((TextView) view.findViewById(R.id.lv_tv_m1_time)).setText("创建于：" + sdf.format(vo.getLastTime()));
		ImageView img = (ImageView) view.findViewById(R.id.lv_img_m1_pic);
		img.setImageResource(R.drawable.pack);
		view.setTag(vo);
		return view;
	}

	// 用于监听手机的退出事件，彻底退出程序
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			MsgShow.showExitApp(ChengJiFileListActivity.this);
		}
		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Const.ChengJiFileListActivity_code) {
			if (resultCode == Const.ChengJiEditActivity_code) {
				String msg = data.getExtras().getString("msg");
				MyLog.i(TAG, msg);
				refresh();
			}
		}
	}
}
